//
//  HZBaseListViewController.m
//  HZ
//  需要自己手动添加scrollview 或者tableview的分页请求的 基类view
//  Created by huazi on 14-8-5.
//  Copyright (c) 2014年 HZ. All rights reserved.
//

#import "HZBaseListViewController.h"
#import "MJRefresh.h"
@interface HZBaseListViewController ()<UIScrollViewDelegate>
{
    UIScrollView *_dataScrollView;
    NSInteger _totalPages;
}
@end

@implementation HZBaseListViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.currentPage =0;
    self.pageDataArr =[NSMutableArray new];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)setTotalPages:(NSInteger)totalPage
{
    _totalPages =totalPage;
}
#pragma mark - Refresh Control And Request Latest Data

- (void)addRefreshHeader:(UIScrollView *)scrollView
{
    _dataScrollView =scrollView;
//    _dataScrollView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//        
//        [scrollView.mj_header endRefreshing];
//        
//    }];
    _dataScrollView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRereshing)];
    [_dataScrollView.mj_header beginRefreshing];
    
//    [_dataScrollView addHeaderWithTarget:self action:@selector(headerRereshing)];
//#warning 自动刷新(一进入程序就下拉刷新)
//    [_dataScrollView headerBeginRefreshing];
//    
//    
//    // 设置文字(也可以不设置,默认的文字在MJRefreshConst中修改)
//    _dataScrollView.headerPullToRefreshText = @"下拉可以刷新了";
//    _dataScrollView.headerReleaseToRefreshText = @"松开马上刷新了";
//    _dataScrollView.headerRefreshingText = @"正在帮你刷新中";
    
}
- (void)addRefreshHeaderAndFooterView:(UIScrollView *)scrollView
{
    _dataScrollView =scrollView;
     _dataScrollView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRereshing)];
    _dataScrollView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRereshing)];
    [_dataScrollView.mj_header beginRefreshing];
    
    
    
//    [_dataScrollView addHeaderWithTarget:self action:@selector(headerRereshing)];
//#warning 自动刷新(一进入程序就下拉刷新)
//    [_dataScrollView headerBeginRefreshing];
//    
//    // 2.上拉加载更多(进入刷新状态就会调用self的footerRereshing)
//    [_dataScrollView addFooterWithTarget:self action:@selector(footerRereshing)];
//    
//    // 设置文字(也可以不设置,默认的文字在MJRefreshConst中修改)
//    _dataScrollView.headerPullToRefreshText = @"下拉可以刷新了";
//    _dataScrollView.headerReleaseToRefreshText = @"松开马上刷新了";
//    _dataScrollView.headerRefreshingText = @"正在帮你刷新中";
//    
//    if (self.isDefault)
//    {
//        _dataScrollView.footerPullToRefreshText = @"上拉可以加载全部数据";
//        _dataScrollView.footerReleaseToRefreshText = @"松开马上加载全部数据了";
//        _dataScrollView.footerRefreshingText = @"";
//    }
//    else
//    {
//        _dataScrollView.footerPullToRefreshText = @"上拉可以加载更多数据了";
//        _dataScrollView.footerReleaseToRefreshText = @"松开马上加载更多数据了";
//        _dataScrollView.footerRefreshingText = @"正在帮你加载中";
//    }
    
}
- (void)endRefreshControlLoading
{
//    if (self.currentPage ==0)
//    {
//        [_dataScrollView headerEndRefreshing];
//    }
//    else {
//        [_dataScrollView footerEndRefreshing];
//    }
}
#pragma mark -override
- (void)loadDataWithCurrentpage:(NSInteger)currentpage
{
    
}
- (void)loadAllDataWithOtherVC
{
    
}
- (void)headerRereshing
{
    [self resetFirstLoadData];
    [self loadDataWithCurrentpage:self.currentPage];
}
- (void)footerRereshing
{
    if (self.currentPage < _totalPages)
    {
        self.currentPage++;
        [self loadDataWithCurrentpage:self.currentPage];
    } else
    {
        if (self.isDefault)
        {
            [_dataScrollView.mj_footer endRefreshing];
            [self loadAllDataWithOtherVC];
        }
        else
        {
//            [_dataScrollView.mj_header endRefreshing];
            [_dataScrollView.mj_footer endRefreshing];
            [self.view makeToast:@"没有更多的数据" duration:1.0 position:@"center"];
        }
    }
    
}
- (void)resetFirstLoadData
{
    self.currentPage =0;
    [self.pageDataArr removeAllObjects];
    [(UITableView *)_dataScrollView reloadData];
}

@end
